package com.taotao.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.taotao.common.utils.Constants;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.DateUtil;
import com.taotao.mapper.EyAddressMapper;
import com.taotao.mapper.EyChargeMapper;
import com.taotao.mapper.EyMemberMapper;
import com.taotao.pojo.EyCharge;
import com.taotao.pojo.EyChargeExample;
import com.taotao.pojo.EyMember;
import com.taotao.service.ChargeService;
import com.taotao.service.OperateLogService;
import com.taotao.service.WechatService;

@Service
public class ChargeServiceImpl implements ChargeService{

	@Autowired
	EyAddressMapper eyAddressMapper;
	@Autowired
	WechatService wechatService;
	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	EyChargeMapper eyChargeMapper;
	@Autowired
	OperateLogService operateLogService;
	
	BaseResult baseResult = new BaseResult();
	@Override
	public Map<String, Object> chargeAdd(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String chargeDesc = map.get("chargeDesc");
			String priceTotal = map.get("priceTotal");
			String priceDiscount = map.get("priceDiscount");
			String pricePay = map.get("pricePay");
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			if(member==null){
				return baseResult.getErrorMap("会员编号不存在,memberId:"+memberId);
			} 
			
			BigDecimal bd_priceTotal = new BigDecimal(priceTotal);
			
			String balance = member.getBalance();
			if(StringUtils.isBlank(balance)){
				balance=Constants.num_0;
			}
			
			BigDecimal bd_balance = new BigDecimal(balance);
			bd_balance=bd_priceTotal.add(bd_balance);
			
			EyCharge charge = new EyCharge();
			charge.setMemberId(Integer.parseInt(memberId));
			charge.setType(Constants.num_1);  //1充值  2消费
			charge.setPriceTotal(priceTotal);
			charge.setPriceDiscount(priceDiscount);
			charge.setChargeDesc(chargeDesc);
			charge.setPricePay(pricePay);
			charge.setBalance(String.valueOf(bd_balance));
			charge.setCreateTime(DateUtil.getDateAndTime());
			charge.setUpdateTime(DateUtil.getDateAndTime());
			eyChargeMapper.insert(charge);
			
			member.setBalance(String.valueOf(bd_balance));
			member.setUpdateTime(DateUtil.getDateAndTime());
			eyMemberMapper.updateByPrimaryKey(member);
			
			operateLogService.insert(Integer.parseInt(memberId), null,null,OperateType.operateType_40.getDesc(),OperateType.operateType_40.getCode(),""); 
			
			Map<String,String> m=new HashMap<>();
			m.put("chargeId", charge.getId().toString());
			return baseResult.getSuccMap(m);
			
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	
	@Override
	public Map<String, Object> chargeSubtract(EyMember member,String payMoney,String desc) {
		try {
			String balance=member.getBalance(); //余额
			balance=StringUtils.isBlank(balance)?"0.00":balance;
			BigDecimal balance2 = new BigDecimal(balance);
			
			BigDecimal pay=new BigDecimal(payMoney);
			if(balance2.compareTo(pay)< 0){ //余额不足
				return baseResult.getErrorMap("-3","The balance is insufficient, please recharge it");
			}
			BigDecimal balanceNew = balance2.subtract(pay);
			member.setBalance(String.valueOf(balanceNew));
			member.setUpdateTime(DateUtil.getDateAndTime());
			eyMemberMapper.updateByPrimaryKey(member);
			
			EyCharge charge = new EyCharge();
			charge.setMemberId(member.getId());
			charge.setType(Constants.num_2); //1充值  2消费
			charge.setPriceTotal(payMoney);
			charge.setPriceDiscount(Constants.num_0);
			charge.setChargeDesc(desc);
			charge.setPricePay(payMoney);
			charge.setBalance(String.valueOf(balanceNew));
			charge.setCreateTime(DateUtil.getDateAndTime());
			charge.setUpdateTime(DateUtil.getDateAndTime());
			eyChargeMapper.insert(charge);
			
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	
	@Override
	public Map<String, Object> getChargeList(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String type = map.get("type");
			String pageNum =  map.get("pageNum");
			String pageSize = map.get("pageSize");
			
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
			List<EyCharge> list=null;
			if(StringUtils.isNotBlank(memberId)){
				EyChargeExample example = new EyChargeExample();
				com.taotao.pojo.EyChargeExample.Criteria criteria = example.createCriteria();
				criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
				criteria.andTypeEqualTo(type);
				example.setOrderByClause(" create_time desc ");
				PageHelper.startPage(num,size);
				list = eyChargeMapper.selectByExample(example);
				for(EyCharge c:list){
					String createTime=DateUtil.getDateTimeFormatStr(c.getCreateTime());
	  				c.setCreateTime(createTime);
	  			}
			}
		
			PageInfo<EyCharge> pageInfo = new PageInfo<>(list);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}


}
